package net.zzy.pbbot.plugin.zfbSign.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.lz1998.pbbot.utils.Msg;
import net.zzy.pbbot.plugin.zfbSign.entity.ZfbSaveResp;
import net.zzy.pbbot.plugin.zfbSign.entity.ZfbSignDaoEntity;
import net.zzy.pbbot.plugin.zfbSign.mapper.ZfbSignMapper;
import net.zzy.pbbot.plugin.zfbSign.tool.ZfbUtil;
import net.zzy.pbbot.util.HttpsUtils;
import net.zzy.pbbot.util.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static net.zzy.pbbot.finalfield.FinalStr.NEED_SET_ZFB_INFO;
import static net.zzy.pbbot.finalfield.FinalStr.SET_ZFB_INFO_SUC;

/**
 * @author zhuZhaoYang
 * @date 2021/10/5 23:33
 */
@Service
@Slf4j
public class ZfbSignService {

    private ZfbSignMapper zfbSignMapper;

    @Autowired
    public void setZfbSignMapper(ZfbSignMapper zfbSignMapper) {
        this.zfbSignMapper = zfbSignMapper;
    }


    @SneakyThrows
    public Msg loginAndSave(String qq) {
        ZfbSignDaoEntity zfbSignDaoEntity = zfbSignMapper.selectOne(new QueryWrapper<ZfbSignDaoEntity>().eq("qq", qq));
        if (zfbSignDaoEntity == null) {
            return Msg.builder().text(NEED_SET_ZFB_INFO);
        }
        String loginUrl = UrlUtil.getZfbLoginUrl(zfbSignDaoEntity.getSchoolId(), zfbSignDaoEntity.getStuId());
        HttpsUtils.get(loginUrl, null, null);
        String schoolId = zfbSignDaoEntity.getSchoolId();
        String resp = HttpsUtils.postWithForm(UrlUtil.getZfbSaveUrl(schoolId),
                ZfbUtil.getSaveHeader(schoolId),
                ZfbUtil.getSaveForm(zfbSignDaoEntity.getProvince(),
                        zfbSignDaoEntity.getCity(),
                        zfbSignDaoEntity.getDistrict(),
                        zfbSignDaoEntity.getStreet(),
                        zfbSignDaoEntity.getLng(),
                        zfbSignDaoEntity.getLat()));
        log.info("提交结果:{}", resp);
        ZfbSaveResp zfbSaveResp = JSON.parseObject(resp, ZfbSaveResp.class);
        return Msg.builder().text(zfbSaveResp.getMsg());
    }

    public Msg setInfo(String[] messages, String qq) {
        if (messages.length != 9) {
            return Msg.builder().text("参数有误!");
        }
        String schoolId = messages[1];
        String stuId = messages[2];
        String province = messages[3];
        String city = messages[4];
        String district = messages[5];
        String street = messages[6];
        String lng = messages[7];
        String lat = messages[8];

        ZfbSignDaoEntity zfbSignDaoEntity = new ZfbSignDaoEntity();
        zfbSignDaoEntity.setSchoolId(schoolId);
        zfbSignDaoEntity.setStuId(stuId);
        zfbSignDaoEntity.setProvince(province);
        zfbSignDaoEntity.setCity(city);
        zfbSignDaoEntity.setDistrict(district);
        zfbSignDaoEntity.setStreet(street);
        zfbSignDaoEntity.setLng(lng);
        zfbSignDaoEntity.setLat(lat);
        zfbSignDaoEntity.setQq(qq);

        zfbSignMapper.insert(zfbSignDaoEntity);
        return Msg.builder().text(SET_ZFB_INFO_SUC);
    }

    public Msg deleteInfo(String qq) {
        ZfbSignDaoEntity zfbSignDaoEntity = zfbSignMapper.selectOne(new QueryWrapper<ZfbSignDaoEntity>().eq("qq", qq));
        if (zfbSignDaoEntity == null) {
            return Msg.builder().text(NEED_SET_ZFB_INFO);
        }
        zfbSignMapper.deleteById(zfbSignDaoEntity.getId());
        return Msg.builder().text("已删除信息");
    }
}
